
MgHUD_Speedometer = 
{
    userData = {}
}
registerWidget("MgHUD_Speedometer");


function MgHUD_Speedometer:draw()

    ------ Early out if HUD shouldn't be shown. ------
    if not shouldShowHUD() then return end

    ------ GET PLAYER ------
    local player = getPlayer();

    ------ OPTIONS ------
    local enableSpeedBar = true;
    local enableSpeedNum = true;
    local speedX = 0;
    local speedY = 15;
    local barWidth = 250;
    local barHeight = 10;
    local barAlpha = 128;

    ------ SHORTHAND VARIABLES ------
    local speed = math.floor(player.speed);
    local barLeft = -(barWidth/2);
    local textX = speedX;
    local textY = speedY;
    local barRight = -barLeft
    local barTop = 40

    ------ TEXT FORMATTING OPTIONS ------
    nvgTextAlign(NVG_ALIGN_CENTER, NVG_ALIGN_TOP);
    nvgFontFace("CodeProLC");
    nvgFontSize(30);

    ------ DRAW BAR ------
    if enableSpeedBar then
        if (speed > 400) then
            local speed = (speed-400)/250*barWidth
            nvgBeginPath()
            nvgFillColor(Color(255,255,0,barAlpha))
            nvgRect(barLeft, barTop, math.min(speed, barWidth), barHeight);
            nvgFill()
        end
        if (speed > 650) then
            local speed = (speed-650)/250*barWidth
            nvgBeginPath()
            nvgFillColor(Color(255,0,0,barAlpha))
            nvgRect(barLeft, barTop, math.min(speed, barWidth), barHeight);
            nvgFill()
        end
    end

    ------ DRAW TEXT ------
    if enableSpeedNum and speed > 0 then
        -- offset for shadow doesn't use the formula,
        -- as the number is too small
        nvgFillColor(Color(48,48,48))
        nvgText(speedX+2.5, speedY+1.25, speed);
        nvgFillColor(Color(255,255,255))
        nvgText(speedX, speedY, speed);
    end

end
